package com.bruce.starts.demo.jdk.jdk8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


public class StreamDemo {
    private static List<Dish> menu = Arrays.asList(
            new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT),
            new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("french fries", true, 530, Dish.Type.OTHER),
            new Dish("rice", true, 350, Dish.Type.OTHER),
            new Dish("season fruit", true, 120, Dish.Type.OTHER),
            new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 300, Dish.Type.FISH),
            new Dish("salmon", false, 450, Dish.Type.FISH) );
    public static void main(String[] args) {
        //System.out.println(menu.stream().mapToInt(Dish::getCalories).max().getAsInt());
        //System.out.println(menu.stream().collect(Collectors.reducing(0,Dish::getCalories,Integer::sum)));
        //filter();
        //Map<Dish.Type,Dish> groupType = menu.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.collectingAndThen(
        //        Collectors.maxBy(Comparator.comparing(Dish::getCalories)),Optional::get
        //)));
        //System.out.println(groupType);
        test1();;
    }

    private static void filter(){
        List<Dish> specialMenu = Arrays.asList(
                new Dish("seasonal fruit", true, 120, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER));
        List<Dish> filterMenu = specialMenu.stream().takeWhile(dish -> dish.getCalories()<320).collect(Collectors.toList());
        var test = 1;
        System.out.println(filterMenu);
        System.out.println(test);
    }

    private static void test1(){
        List<String> list = new ArrayList<>();
        list.add("1");
        Optional<String> optional = Optional.ofNullable(list).flatMap(l -> l.stream().findFirst());
        System.out.println(optional.orElse("hello"));
    }
}
